package cn.xdmnb.cloud.blog.core.model;

import cn.xdmnb.cloud.blog.core.dict.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Objects;

/**
 * @author yukai
 * @since 2023年01月05日 15:59
 */
@ApiModel("统一响应")
public class ResultMsg<T> {

    @ApiModelProperty("业务码")
    private Integer code;

    @ApiModelProperty("响应消息")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    public static <T> ResultMsg<T> resultSuccess(){
        return new ResultMsg<>(ResultCode.REQUEST_SUCCESS.getCode(),ResultCode.REQUEST_SUCCESS.getMsg(),null);
    }

    public static ResultMsg resultFail(){
        return new ResultMsg<>(ResultCode.UNAUTHORIZED.getCode(),ResultCode.UNAUTHORIZED.getMsg(),null);
    }

    public static <T>  ResultMsg<T> resultFail(Integer code,String msg){
        return new ResultMsg<>(code,msg,null);
    }


    public static boolean isSuccess(ResultMsg<?> resultMsg){
        return Objects.nonNull(resultMsg)&&resultMsg.getCode().equals(ResultCode.REQUEST_SUCCESS.getCode());
    }

    public static <T> ResultMsg<T> resultSuccess(T data){
        return new ResultMsg(ResultCode.REQUEST_SUCCESS.getCode(),ResultCode.REQUEST_SUCCESS.getMsg(),data);
    }

    private ResultMsg(){}

    public ResultMsg(Integer code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
